<?php
/**
 * Created by PhpStorm.
 * User: locove
 * Date: 15-5-21
 * Time: 上午11:31
 * 库存储位实时查询
 */
class Controller_Storsearch extends Controller_Admin {
    public function before()
    {
        try {
            ACL::Required('storsearch');
        } catch ( HTTP_Exception_403 $e ) {
            Message::error('sorry，你没有权限进这个目录');
            $this->request->redirect ( URL::base (true) );
        }

        parent::before();
    }

    public function action_index() {
        $this->title = '库存储位实时查询';
        $view        = View::factory('storsearch');

        $stor = new Storsearch();

        empty($_GET['download']) || $stor->download = 1;

        $view->set('page',$page = $this->page($stor->doCondition()));

        empty($_GET['download']) || $this->download($stor::$lists);

        $stor->page = $page;

        $stor->getLimitLists();

        $view->set('lists', $stor::$lists)
            ->set('get', $_GET)
            ->set('num', $stor::$num);

        $stor::$lists = array();

        $this->response->body($view);
    }

    public function page($count = NULL)
    {
        return Pagination::factory(array(
                'total_items'    => empty($count)?0:$count,
                'items_per_page' => 10,
                'uri'            => $this->request->uri()
            ));
    }

    public function download($lists) {
        set_time_limit(0);
        ini_set('memory_limit','2048M');

        require_once(Kohana::find_file('vendor','PHPExcel/PHPExcel/IOFactory'));
        $this->objReader = PHPExcel_IOFactory::createReader('Excel5');
        $objPHPExcel     = new PHPExcel();
        $objPHPExcel->getProperties()
            ->setCreator("YKS")
            ->setLastModifiedBy("YKS")
            ->setTitle("Office 2003 XLSX Test Document")
            ->setSubject("Office 2003 XLSX Test Document")
            ->setDescription("Test document for Office 2003 XLSX, generated using PHP classes.")
            ->setKeywords("office 2003 openxml php")
            ->setCategory("Test result file");
        $objActSheet = $objPHPExcel->setActiveSheetIndex(0);
        $objActSheet->setCellValue('A1','SKU');
        $objActSheet->setCellValue('B1','中文名称');
        $objActSheet->setCellValue('C1','储位');
        $objActSheet->setCellValue('D1','实际库存');
        $objActSheet->setCellValue('E1','销售状态');

        $i        = 0;
        foreach($lists as $list){
            $key = $i+2;
            $objActSheet->setCellValue('A'.$key,$list['sku']);
            $objActSheet->setCellValue('B'.$key,$list['name']);
            $objActSheet->setCellValue('C'.$key,$list['storage']);
            $objActSheet->setCellValue('D'.$key,$list['stock']);
            $objActSheet->setCellValue('E'.$key,$list['status']);
            $i++;
        }

        $objPHPExcel->getActiveSheet()
            ->setTitle('库存储位实时查询');
        $objPHPExcel->setActiveSheetIndex(0);
        $day      = date("m-d");
        $filename = $day."库存储位实时查询.xls";
        ob_end_clean();
        header("Content-Type: application/vnd.ms-excel; charset=utf-8");
        header('Content-Disposition: attachment;filename='.$filename);
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5');
        $objWriter->save('php://output');
        exit;
    }
}